package com.wish.house.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.common.core.utils.StringUtils;
import com.wish.common.core.utils.poi.ExcelUtil;
import com.wish.common.core.web.controller.BaseController;
import com.wish.common.core.web.domain.AjaxResult;
import com.wish.common.core.web.page.TableDataInfo;
import com.wish.common.log.annotation.Log;
import com.wish.common.log.enums.BusinessType;
import com.wish.common.security.annotation.RequiresPermissions;
import com.wish.house.entity.HousesPremises;
import com.wish.house.service.IHousesPremisesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 楼盘 前端控制器
 * </p>
 *
 * @author hyx
 * @since 2022-11-23
 */
@RestController
@RequestMapping("/premises")
public class HousesPremisesController extends BaseController {

    @Autowired
    IHousesPremisesService housesPremisesService;

    /**
     * 查询楼盘列表
     */
    @RequiresPermissions("house:premises:list")
    @GetMapping("/list")
    public TableDataInfo list(HousesPremises tbHousesPremises)
    {
        QueryWrapper<HousesPremises> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(tbHousesPremises.getHousesName())) {
            queryWrapper.like("houses_name", tbHousesPremises.getHousesName());
        }
        startPage();
        List<HousesPremises> list = housesPremisesService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出楼盘列表
     */
    @RequiresPermissions("house:premises:export")
    @Log(title = "楼盘", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HousesPremises tbHousesPremises)
    {
        List<HousesPremises> list = housesPremisesService.list();
        ExcelUtil<HousesPremises> util = new ExcelUtil<HousesPremises>(HousesPremises.class);
        util.exportExcel(response, list, "楼盘数据");
    }

    /**
     * 获取楼盘详细信息
     */
    @RequiresPermissions("house:premises:query")
    @GetMapping(value = "/{housesId}")
    public AjaxResult getInfo(@PathVariable("housesId") Integer housesId)
    {
        return success(housesPremisesService.getById(housesId));
    }

    /**
     * 新增楼盘
     */
    @RequiresPermissions("house:premises:add")
    @Log(title = "楼盘", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HousesPremises tbHousesPremises)
    {
        tbHousesPremises.setCreateTime(LocalDateTime.now());
        return toAjax(housesPremisesService.save(tbHousesPremises));
    }

    /**
     * 修改楼盘
     */
    @RequiresPermissions("house:premises:edit")
    @Log(title = "楼盘", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HousesPremises tbHousesPremises)
    {
        tbHousesPremises.setUpdateTime(LocalDateTime.now());
        return toAjax(housesPremisesService.updateById(tbHousesPremises));
    }

    /**
     * 删除楼盘
     */
    @RequiresPermissions("house:premises:remove")
    @Log(title = "楼盘", businessType = BusinessType.DELETE)
    @DeleteMapping("/{housesIds}")
    public AjaxResult remove(@PathVariable Integer[] housesIds)
    {
        return toAjax(housesPremisesService.removeBatchByIds(Arrays.asList(housesIds)));
    }

}
